package com.light.mytext.thread

import android.os.Build
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.annotation.RequiresApi
import androidx.appcompat.app.AppCompatActivity
import com.light.mytext.R
import kotlinx.android.synthetic.main.activity_thread_local.*
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*
import java.util.concurrent.Executors
import java.util.function.Supplier

class ThreadLocalActivity : AppCompatActivity(), View.OnClickListener {
    lateinit var THREAD_LOCAL_NUM: ThreadLocal<Int?>
    var stringBuffer = StringBuffer("")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_thread_local)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            Log.i("ThreadLocal==", "Build.VERSION.SDK_INT="+Build.VERSION.SDK_INT+"Build.VERSION_CODES.O="+Build.VERSION_CODES.O)
        }
        init()
        init_Listener()
    }

    fun init() {
        //线程本地存储变量
        THREAD_LOCAL_NUM = object : ThreadLocal<Int?>() {
            override fun initialValue(): Int {
                return 0
            }
        }
    }

    fun init_Listener() {
        bt_0.setOnClickListener(this)
    }

    @RequiresApi(Build.VERSION_CODES.O)
    fun main() {
        var service = Executors.newFixedThreadPool(20)
        for (i in 0..19) {
            service.execute {
                Log.i("ThreadLocal==", getChangeData("2019-06-01 16:34:30").toString())
            }
        }
        service.shutdown()
    }


    @RequiresApi(Build.VERSION_CODES.O)
    override fun onClick(v: View?) {
        main()
    }

    /**
     * 日期转换
     */
    var sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
   /* @RequiresApi(Build.VERSION_CODES.O)
    internal  var threadLocal=ThreadLocal.withInitial {
        SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
    }*/

//    @RequiresApi(Build.VERSION_CODES.O)
//    private var threadLocal1: ThreadLocal<SimpleDateFormat> = ThreadLocal.withInitial { SimpleDateFormat("yyyy-MM-dd HH:mm:ss") }


    @RequiresApi(Build.VERSION_CODES.O)
    fun getChangeData(dataStr: String): Date? {
        var date: Date? = null
        try {
//            threadLocal1.get()!!.parse(dataStr)
        } catch (e: ParseException) {
            e.printStackTrace()
        }
        return date
    }

}